// pages/user-profile/user-profile.js
const app = getApp()
const { request } = require("../../utils/request")

Page({
  data: {
    urlPath: "",
    name: "",
    point: 0, // 初始积分
  },
  getPath() {
    try {
      const tokenName = wx.getStorageSync("tokenName")
      console.log("tokenName:", tokenName)
      if (tokenName === "usertoken") {
        this.setData({ urlPath: "employee" })
      } else if (tokenName === "admintoken") {
        this.setData({ urlPath: "admin" })
      } else {
        this.setData({ urlPath: "sadmin" })
      }
    } catch (e) {
      console.error("获取 tokenName 失败:", e)
      // this.setData({ urlPath: "sadmin" }); // 默认值
    }
  },
  onLoad() {
    this.init()
  },
  init() {
    this.getPath()
    const id = wx.getStorageSync("userId")
    const { urlPath } = this.data
    request({
      url: `${app.globalData.baseUrl}/${urlPath}/profile/${id}`,
      method: "GET",
    })
      .then((res) => {
        this.setData({
          name: res.data.data.name,
          point: res.data.data.point,
        })
      })
      .catch(() => {
        wx.showToast({ title: "网络错误", icon: "none" })
      })
  },

  // 跳转到订单列表
  navigateToOrders() {
    wx.navigateTo({
      url: "/pages/order_list/order_list",
    })
  },

  // 跳转到员工列表
  navigateToEmployeeList() {
    wx.navigateTo({
      url: "/pages/employee_list/employee_list",
    })
  },

  // 跳转到管理员列表
  navigateToAdminList() {
    wx.navigateTo({
      url: "/pages/admin_list/admin_list",
    })
  },

  navigateToCompanyList() {
    wx.navigateTo({
      url: "/pages/company_list/company_list",
    })
  },

  // 跳转到订单查询页面（超级管理员）
  navigateToOrderListSadmin() {
    wx.navigateTo({
      url: "/pages/order_list_sadmin/order_list_sadmin",
    })
  },

  // 跳转到订单管理
  navigateToOrdersManage() {
    wx.navigateTo({
      url: "/pages/order_list_admin/order_list_admin",
    })
  },

  navigateToRoyaltyList() {
    wx.navigateTo({
      url: "/pages/royalty/royalty",
    })
  },

  logout() {
    const { urlPath } = this.data
    wx.showModal({
      // 显示确认对话框
      title: "确认退出",
      content: "确定要退出登录吗？",
      success: (res) => {
        if (res.confirm) {
          // 用户点击确定
          console.log("用户点击确定")

          request({
            url: `${app.globalData.baseUrl}/${urlPath}`,
            method: "DELETE",
          })

        //   const app = getApp()
          app.clearLoginInfo()

          // 2. 跳转到登录页面
          wx.reLaunch({
            // 使用 reLaunch 关闭所有页面并跳转
            url: "/pages/login/login", // 替换为你的登录页面路径
          })

          wx.showToast({
            title: "退出成功",
            icon: "success",
            duration: 2000,
          })
        } else if (res.cancel) {
          // 用户点击取消
          console.log("用户点击取消")
        }
      },
    })
  },
  onShow() {
    this.getTabBar().init()
    this.init()
  },
  goToPhoneManage() {
    // 检查是否是超级管理员
    const tokenName = wx.getStorageSync('tokenName')
    if (tokenName === 'sadmintoken') {
      wx.navigateTo({
        url: '/pages/phone_manage/phone_manage'
      })
    } else {
      wx.showToast({
        title: '无权限访问',
        icon: 'none'
      })
    }
  },
})
